/**
 *  编辑页
 *
 * 作者 zhangwei
 * 日期 2024-09-02 11:25:48
 */
layui.use(['jquery','febs', 'form', 'validate'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        $view = $('#hazardExamineTeam-edit'),
        $correlationUserSelect = $('#correlationUserSelect'),
        validate = layui.validate;
    let tenantId = POPUP_DATA.tenantId;
    let selectUsers = [];
    form.verify(validate);
    form.render();
    initFormValue();

    function initFormValue() {
        if (hazardExamineTeamData) {
            form.val("hazardExamineTeam-edit-form", {
                "examineTeamId": hazardExamineTeamData.examineTeamId,
                "tenantId": hazardExamineTeamData.tenantId,
                "teamName": hazardExamineTeamData.teamName,
                "remark": hazardExamineTeamData.remark,
                "sort": hazardExamineTeamData.sort,
                "status": hazardExamineTeamData.status,
                "teamUserList": hazardExamineTeamData.teamUserList,
            });
            if(hazardExamineTeamData.status=="1"){
                $view.find("#status").prop("checked","checked")
            }else{
                $view.find("#status").prop("checked","")
            }
            if(hazardExamineTeamData.teamUserList){
                let data = hazardExamineTeamData.teamUserList;
                let htl = ``;
                if (data && data.length > 0) {
                    data.forEach((item, index) => {
                        selectUsers.push({userId:item.userId});
                        htl += `<span name="correlation" 
                                            data-id="${item.userId}" 
                                            data-uname="${item.realName}" 
                                            data-userName="${item.userName}" 
                                            data-userNo="${item.userNo}" 
                                            data-userDeptId="${item.userDeptId}" 
                                            data-userDeptName="${item.userDeptName}" 
                                            data-userPostId="${item.userPostId}" 
                                            data-userPostName="${item.userPostName}" 
                                            class="tag" >${item.realName}(${item.userNo}/${item.userDeptName})<span style="color: red;margin-left: 5px;cursor:pointer;" onclick="delTags(this)">x</span></span>`
                    })
                }
                $('.selectName').html(htl)
            }
            form.render()
        }
    }

    form.on('submit(hazardExamineTeam-edit-form-submit)', function (data) {
        if(data.field.status == "on"){
            data.field.status = '1';
        }else{
            data.field.status = '0';
        }
        addOrUpdate(data.field);
        return false;
    });

    //小组成员选择点击事件
    $correlationUserSelect.on('click', function () {
        febs.modal.open('小组成员选择', 'common/user', {
            btn: ['确认','关闭'],
            area: $(window).width() <= 750 ? ['95%', '85%']: ['80%', '85%'],
            data:{
                tenantId:tenantId,
                selectList:selectUsers,
                multiple:true,
            },
            yes: function (index, layero) {
                let selectData = null
                if(typeof getSelectData  === 'function'){
                    selectData = getSelectData()
                }
                if(selectData){
                    let data = getSelectData()
                    let htl = ``;
                    if (data && data.length > 0) {
                        data.forEach((item, index) => {
                            selectUsers.push({userId:item.userId});
                            htl += `<span name="correlation" class="tag" 
                                            data-id="${item.userId}" 
                                            data-uname="${item.uname}" 
                                            data-userName="${item.userName}" 
                                            data-userNo="${item.userNo}" 
                                            data-userDeptId="${item.deptId}" 
                                            data-userDeptName="${item.deptName}" 
                                            data-userPostId="${item.postId}" 
                                            data-userPostName="${item.postName}" >${item.uname}(${item.userNo}/${item.deptName})<span style="color: red;margin-left: 5px;cursor:pointer;" onclick="delTags(this)">x</span></span>`
                        })
                    }
                    $('.selectName').append(htl)
                    layer.close(index);
                }
                layer.close(index);
            },
            btn2: function (index, layero) {
                layer.close(index);
                return false;
            }
        });
    });

    window.delTags = function (data) {
        $(data).parent().remove();
        var removeUserId = $(data).parent().attr("data-id");
        selectUsers = selectUsers.filter(item => item.userId != removeUserId);
    }
    function addOrUpdate(data) {
        data.tenantId = tenantId;
        //小组人员
        let tagList =  $('.selectName').find('.tag');
        if(tagList.length>0){
            let unameList = [];
            for (let i = 0; i < tagList.length; i++) {
                let userId = $($(tagList)[i]).attr("data-id")
                let userName = $($(tagList)[i]).attr("data-userName")
                let uname = $($(tagList)[i]).attr("data-uname")
                let userNo = $($(tagList)[i]).attr("data-userNo")
                let userDeptId = $($(tagList)[i]).attr("data-userDeptId")
                let userDeptName = $($(tagList)[i]).attr("data-userDeptName")
                let userPostId = $($(tagList)[i]).attr("data-userPostId")
                let userPostName = $($(tagList)[i]).attr("data-userPostName")
                let temp = {
                    userId:userId,
                    userName:userName,
                    realName:uname,
                    userNo:userNo,
                    userDeptId:userDeptId,
                    userDeptName:userDeptName,
                    userPostId:userPostId,
                    userPostName:userPostName,
                };
                unameList.push(temp);
            }
            data.teamUserList = unameList;
        }else {
            febs.alert.warn('请选择小组成员');
            return ;
        }
        febs.modal.confirm("保存提示！","是否保存？",function (index){
            if (data.examineTeamId && data.examineTeamId.length > 0) {
                febs.postArray(ctx + 'hazardExamineTeam/update', data, function () {
                    layer.closeAll();
                    febs.alert.success('修改成功');
                });
            } else {
                febs.postArray(ctx + 'hazardExamineTeam', data, function () {
                    layer.closeAll();
                    febs.alert.success('新增成功');
                });
            }
            $('#febs-hazardExamineTeam').find('#query').trigger('click');
        })
    }
});